<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Manage extends Controller {

// Used for registering and changing password form validation
    var $min_username = 2;
    var $max_username = 20;
    var $min_password = 4;
    var $max_password = 20;

    function Manage() {
        parent::Controller();
        $this->load->library('DX_Auth');
        $this->load->library('user_agent');
        $this->load->library('Form_validation');

        $this->load->helper('form');

        $this->load->helper(array('form', 'url'));
        
    }

    function index() {
    //代码
        if (!$this->dx_auth->is_admin()) {
            redirect('/auth/login/','location');
        };
        $data['browser'] = $this->agent->browser();

        $this->load->view('admin/system',$data);
        $this->adduser();
    }

   ////添加用户/////
    function adduser() {
        $data['adduser_messege'] = '';
    //判断是否登录
        if ( $this->dx_auth->is_logged_in() AND $this->dx_auth->allow_registration) {
        //判断是否提交
            if ($this->input->post('email')) {
            //判断提交的email是否重复
                if($this->dx_auth->is_email_available($this->input->post('email'))) {

                    $val = $this->form_validation;
                    // Set form validation rules
                    $val->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->min_username.']|max_length['.$this->max_username.']|callback_username_check|');
                    $val->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']|matches[confirm_password]');
                    $val->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean');
                    $val->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_email_check');
                    $val->set_rules('role','Role');
                    // Run form validation and register user if it's pass the validation
                    //如果添加成功
                    if ($val->run() AND $this->dx_auth->register($val->set_value('username'), $val->set_value('password'), $val->set_value('email')) AND !$this->dx_auth->is_email_available($this->input->post('email')) ) {
                        $data['adduser_messege'] = '添加成功!';
                        $data['reset']=true;
                    }else {
                    //否则
                        $data['adduser_messege'] = '添加失败';
                    }
                //倘若email已存在
                }else {
                    $data['adduser_messege'] = '邮箱已存在';
                }
            }
            //无论怎样都载入试图
            $this->load->view('admin/adduser',$data);
        }else{
            //未登录重定向页面到登录
            redirect('/auth/login/','location');
        }
        
    }
    //添加班级
    function addclass() {
        $this->load->model('dx_auth/users', 'users');
        $data['teacherid']=$this->users->get_user_by_roles(3)->result();
        $this->load->view('admin/addclass',$data);
    }
    
    //管理班级
    function manageclass(){
        $this->load->model('dx_auth/users', 'users');
        $data['classid']=$this->users->getClass_by_id()->result();
        $data['search']=true;
        if($this->input->post('classid')){
            $data['search']=false;
        }
        $this->load->view('admin/manageclass',$data);
    }

    //管理用户
    function manageuser() {
        /* Showing page to user */
        $this->load->model('dx_auth/users', 'users');

        // Default role_id that will be showed
        $role_id = $this->input->post('roles') ? $this->input->post('roles') : 1;

        // Get all role from database
        $data['users'] = $this->users->get_user_by_roles($role_id)->result();
        // Get edit and delete permissions
        //$data['edit'] = $this->permissions->get_permission_value($role_id, 'edit');
        //$data['delete'] = $this->permissions->get_permission_value($role_id, 'delete');
        foreach ($_POST as $key => $value) {
        // If checkbox found
            if (substr($key, 0, 9) == 'checkbox_') {
            // If 点击删除
                if (isset($_POST['delete'])) {
                // Ban user based on checkbox value (id)
                    $this->users->delete_user($value);
                }else
                // If 设为内审 button pressed
                    if (isset($_POST['setrole_au'])) {
                    // Ban user based on checkbox value (id)

                        $this->users->set_role($value, 4);
                    }else
                    // If 设为老师 button pressed
                        if (isset($_POST['setrole_tc'])) {
                        // Ban user based on checkbox value (id)

                            $this->users->set_role($value, 3);
                        }else if (isset($_POST['reset_pass'])) {
                            // Set default message
                                $data['reset_message'] = 'Reset password failed';

                                // Get user and check if User ID exist
                                if ($query = $this->users->get_user_by_id($value) AND $query->num_rows() == 1) {
                                // Get user record
                                    $user = $query->row();

                                    // Create new key, password and send email to user
                                    if ($this->dx_auth->forgot_password($user->username)) {
                                    // Query once again, because the database is updated after calling forgot_password.
                                        $query = $this->users->get_user_by_id($value);
                                        // Get user record
                                        $user = $query->row();

                                        // Reset the password
                                        if ($this->dx_auth->reset_password($user->username, $user->newpass_key)) {
                                            $data['reset_message'] = 'Reset password success';
                                        }
                                    }
                                }
                            }
            }
        } 
        //Load View
        //dump($data);
        $this->load->view('admin/manageuser',$data);
    }
     //上传文件
     function do_upload()
        {
              if ($this->input->post("classname")){
                  $classname=$this->input->post("classname");//获取班级名称
                  $teacherid=$this->input->post("teacher");//获取班主任ID
                  $config['upload_path'] = './uploads/';//设置上传路径
                  $config['allowed_types'] = 'csv';//限制上传扩展名
                  $config['file_name'] = 'class';//文件直接重命名
                  $config['overwritee'] =true;//允许覆盖上次上传的
                  $this->load->library('upload',$config);

                  if ( ! $this->upload->do_upload())
                  {
                         $error =  $this->upload->display_errors();
                         $data['info']="上传失败".$error;
                         $data['url']=site_url('admin/manage/addclass');
                         $this->load->view('admin/info',$data);

                  }
                  else
                  {
                         //$data1 = array('upload_data' => $this->upload->data());

                         $filename ="./uploads/class.csv";
                         $this->load->model('admin','parse_csv');
                         $this->parse_csv->parse_csv($filename,$classname,$teacherid);
                         //$data['info']=$string;//"上传成功";
                         //$data['url']=site_url('admin/manage/addclass');
                         //$this->load->view('admin/info',$data);
                  }
              }
              
              
        }
       
}
?>